using DG.Tweening;
using UnityEngine;

namespace Kit.MonoUpdate
{
    public class MovingSquare
    {
        private const float MoveTime = 1f;
        
        public Transform squareTrans;
        private UpdateComp updateComp;

        private float startMoveTime;
        private Vector3 targetPos;

        public void MoveForever()
        {
            if (updateComp == null)
            {
                updateComp = new UpdateComp();
                updateComp.ScheduleAction(MoveLoop, MoveTime, -1);
            }
        }
        
        private void MoveLoop()
        {
            // startMoveTime = Time.time;
            // // 随机选择方向
            // targetPos = squareTrans.position + Mathf.Sign(Random.Range(-1, 1)) * Vector3.right +
            //             Mathf.Sign(Random.Range(-1, 1)) * Vector3.up;
            // var crtPos = squareTrans.position;
            // squareTrans.position = Vector3.Lerp(crtPos, targetPos, (Time.time - startMoveTime) / MoveTime);
            
            targetPos = squareTrans.position + Mathf.Sign(Random.Range(-1, 1)) * Vector3.right +
                        Mathf.Sign(Random.Range(-1, 1)) * Vector3.up;
            squareTrans.transform.DOMove(targetPos, MoveTime).SetEase(Ease.OutSine);
            Debug.Log($"move to target {targetPos}");
        }
    }
}